package com.algorithm;

/**
 * 二叉树的前中后序遍历
 *
 * Created by houjinxin on 16/9/30.
 */
public class TraversalTree {

    public static void main(String[] args) throws Throwable {

        Node llnode = new Node(null, new Node(null, null, "h"), "d");
        Node lrnode = new Node(null, null, "e");

        Node leftNode = new Node(llnode, lrnode, "b");

        Node rlnode = new Node(null, null, "f");
        Node rrnode = new Node(null, null, "g");
        Node rightNode = new Node(rlnode, rrnode, "c");

        Node root = new Node(leftNode, rightNode, "a");

        preOrderTraversal(root);
        System.out.println();
        postOrderTraversal(root);
        System.out.println();
        midOrderTraversal(root);
        System.out.println();

    }

    private static void preOrderTraversal(Node node) {
        if (node == null) {
            return;
        }
        System.out.print(node.getValue().toString());
        preOrderTraversal(node.getLeft());
        preOrderTraversal(node.getRight());
    }

    private static void postOrderTraversal(Node node) {
        if (node == null) {
            return;
        }
        postOrderTraversal(node.getLeft());
        postOrderTraversal(node.getRight());
        System.out.print(node.getValue().toString());
    }

    private static void midOrderTraversal(Node node) {
        if (node == null) {
            return;
        }
        midOrderTraversal(node.getLeft());
        System.out.print(node.getValue().toString());
        midOrderTraversal(node.getRight());
    }
}
